<?php
require_once( 'mofachi.DataConnection.php' );
class LocationData {
	private $field = array();
	public function __get( $key ) {
		if( !isset( $this->field[ $key ] ) ) {
            return null;
        }
		return $this->field[ $key ];
	}
	public function __set( $key, $value ) {
		$this->field[ $key ] = $value;
	}
	public function __construct() {
		$this->field[ "location_id" ] = null;
		$this->field[ "province_code" ] = null;
		$this->field[ "province" ] = null;
		$this->field[ "region_code" ] = null;
		$this->field[ "region" ] = null;
		$this->field[ "city_code" ] = null;
		$this->field[ "city" ] = null;
		$this->field[ "status" ] = null;
	}
	private function loadData( $row ) {
		foreach( $this->field as $key => $value ) {
			$this->field[ $key ] = $row[ $key ];
		}
	}
	public function loadByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from location where location_id=".$this->field[ "location_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
	public function loadByRegionCode() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from location where region_code='".$this->field[ "region_code" ]."'";
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
	public function loadByCityCode() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from location where city_code='".$this->field[ "city_code" ]."'";
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
}
?>